<template>
  <div class="app-container">
    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      size="small"
      style="width: 90%"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column type="selection" width="44px"></el-table-column>
      <!-- <el-table-column label="序号" prop="goodList.ids" align="center" /> -->
      <!-- <el-table-column label="申请入库日期" prop="title" align="center" /> -->
      <el-table-column
        label="产品名称"
        prop="warehouseList[0].spName"
        align="center"
      />
      <el-table-column
        label="产品编号"
        prop="warehouseList[0].spNumber"
        align="center"
      />

      <!-- <el-table-column label="规格型号" prop="bHand" align="center" /> -->
      <el-table-column
        label="单位"
        prop="warehouseList[0].spUnit"
        align="center"
      />
      <el-table-column
        label="待入库数量"
        prop="warehouseList[0].spCount"
        align="center"
      />
      <el-table-column
        label="单价"
        prop="warehouseList[0].spUnit"
        align="center"
      />
      <el-table-column
        label="总金额"
        prop="warehouseList[0].spSumPrice"
        align="center"
      />
      <el-table-column label="生产日期" prop="addTime" align="center" />
      <!-- <el-table-column label="有效日期" prop="bHand" align="center" />
      <el-table-column label="到货日期" prop="bHand" align="center" /> -->
      <!-- <el-table-column label="包装方式" prop="bHand" align="center" /> -->
      <el-table-column label="入库类型" prop="awType" align="center" />
      <el-table-column label="入库库管" prop="awName" align="center" />
      <el-table-column label="入库申请编号" prop="code" align="center" />
      <!-- <el-table-column label="入库申请主题" prop="bHand" align="center" /> -->
    </el-table>
    <pagination
      v-show="totalCount > 0"
      :total="totalCount"
      :page.sync="page"
      :limit.sync="listQuery.MaxResultCount"
      @pagination="getList"
    />
  </div>
</template>
<script>
import Pagination from "@/components/Pagination";
import permission from "@/directive/permission/index.js";
import { cloneDeep } from "lodash";

export default {
  name: "Book",
  components: {
    Pagination,
  },

  directives: {
    permission,
  },

  props: [],
  data() {
    return {
      //新增行
      studentData: [],

      form: {
        id: null,
        bTitle: null,
        bNum: null,
        bPeo: null,
        bTme: null,
        bHand: null,
        bCon: null,

        borrowDetails: [],
      },
      rules: {
        pName: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur",
          },
        ],
      },

      list: [],
      wlist: [],
      totalCount: 0,
      listLoading: true,
      wlistLoading: true,
      warelist: true,
      formLoading: false,
      listQuery: {
        Filter: "",
        Sorting: "",
        SkipCount: 0,
        MaxResultCount: 10,
      },
      page: 1,
      multipleSelection: [],
      formTitle: "",
      isEdit: false,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getList();
    this.getWareList();
  },
  mounted() {},

  methods: {
    getWareListStatus(items, ids) {
      this.$axios
        .posts("api/business/workflow/node-status", ids)
        .then((response) => {
          items.forEach((element) => {
            response.items.filter((i) => {
              if (i.entityId === element.id) {
                element.status = i.status;
                element.nodeId = i.nodeId;
                element.nodeStatus = i.nodeName;
              }
            });
          });
          this.wlist = items;
          this.wlistLoading = false;
          this.listLoading = true;
        });
    },
    getWareList() {
      this.wlistLoading = true;
      this.listLoading = false;
      this.wlistQuery.SkipCount =
        (this.page - 1) * this.wlistQuery.MaxResultCount;
      this.$axios
        .gets("/api/business/ware", this.wlistQuery)
        .then((response) => {
          this.wtotalCount = response.wtotalCount;
          this.getListStatus(
            response.items,
            response.items.map((_) => _.id)
          );
        });
    },

    getListStatus(items, ids) {
      this.$axios
        .posts("api/business/workflow/node-status", ids)
        .then((response) => {
          items.forEach((element) => {
            response.items.filter((i) => {
              if (i.entityId === element.id) {
                element.status = i.status;
                element.nodeId = i.nodeId;
                element.nodeStatus = i.nodeName;
              }
            });
          });
          this.list = items;
          this.listLoading = false;
          this.wlistLoading = true;
        });
    },
    getList() {
      this.listLoading = true;
      this.wlistLoading = true;
      this.listQuery.SkipCount =
        (this.page - 1) * this.listQuery.MaxResultCount;
      this.$axios
        .gets("/api/business/Warehouse/get-all", this.listQuery)
        .then((response) => {
          this.totalCount = response.totalCount;
          this.getListStatus(
            response.items,
            response.items.map((_) => _.id)
          );
        });
    },

    fetchData(id) {
      this.$axios.gets("/api/business/borrow/" + id).then((response) => {
        this.form = response;
      });
    },

    handleFilter() {
      this.page = 1;
      this.getList();
    },
    handleCreate() {
      this.formTitle = "新增图书";
      this.isEdit = false;
      this.dialogVisible = false;
    },
    handleDelete(row) {
      var params = [];
      let alert = "";
      if (row) {
        params.push(row.id);
        alert = row.pName;
      } else {
        if (this.multipleSelection.length === 0) {
          this.$message({
            message: "未选择",
            type: "warning",
          });
          return;
        }
        this.multipleSelection.forEach((element) => {
          let id = element.id;
          params.push(id);
        });
        alert = "选中项";
      }
      this.$confirm("是否删除" + alert + "?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .posts("/api/business/borrow/delete", params)
            .then((response) => {
              this.$notify({
                title: "成功",
                message: "删除成功",
                type: "success",
                duration: 2000,
              });
              this.getList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleCheck() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: "未选择",
          type: "warning",
        });
        return;
      }
      if (this.multipleSelection.length > 1) {
        this.$message({
          message: "暂不支持批量审核",
          type: "warning",
        });
        return;
      }
      this.$confirm("是否审核所选项?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .puts("api/business/workflow/do/" + this.multipleSelection[0].id, {
              data: JSON.stringify(this.multipleSelection[0]),
            })
            .then((response) => {
              this.$notify({
                title: "成功",
                message: "审核成功",
                type: "success",
                duration: 2000,
              });
              this.getList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消审核",
          });
        });
    },
    findFlow(row) {
      this.$router.push({ path: "/tool/flowDisplay/" + row.nodeId });
    },
    handleUpdate(row) {
      this.formTitle = "修改图书";
      this.isEdit = true;
      if (row) {
        this.fetchData(row.id);
      } else {
        if (this.multipleSelection.length != 1) {
          this.$message({
            message: "编辑必须选择单行",
            type: "warning",
          });
          return;
        } else {
          this.fetchData(this.multipleSelection[0].id);
          this.dialogVisible = false;
        }
      }
    },
    save() {
      //数组与对象转换
      let goodList = [];
      this.studentData.forEach((element) => {
        goodList.push({
          id: element.id,
          pName: element.pName,
          pNum: element.pNum,
          sNum: element.sNum,
          bUnit: element.bUnit,
          borNum: element.borNum,
          goBit: element.goBit,
          bitInfo: element.bitInfo,
          giveTime: element.giveTime,
          pageStyle: element.pageStyle,
        });
      });
      this.form.borrowDetails = goodList;

      console.log(this.form);

      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true;
          this.form.roleNames = this.checkedRole;
          if (this.isEdit) {
            this.$axios
              .posts("/api/business/borrow/data-post", this.form)
              .then((response) => {
                this.formLoading = false;
                this.$notify({
                  title: "成功",
                  message: "更新成功",
                  type: "success",
                  duration: 2000,
                });
                this.getList();
              })
              .catch(() => {
                this.formLoading = false;
              });
          } else {
            this.$axios
              .posts("/api/business/borrow/data-post", this.form)
              .then((response) => {
                this.wformLoading = false;
                this.$notify({
                  title: "成功",
                  message: "新增成功",
                  type: "success",
                  duration: 2000,
                });
                this.getList();
              })
              .catch(() => {
                this.formLoading = false;
              });
          }
        }
      });
    },
    sortChange(data) {
      const { prop, order } = data;
      if (!prop || !order) {
        this.handleFilter();
        return;
      }
      this.listQuery.Sorting = prop + " " + order;
      this.handleFilter();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row);
    },

    cancel() {
      this.form = Object.assign({}, defaultForm);
      this.$refs.form.clearValidate();
    },
    GetWare() {
      this.formTitle = "新增图书";
      this.isEdit = false;
      this.dialogVisible = true;
    },
  },
};
</script>
<style></style>
